package com.example.viewtest.AdapterGroup;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.viewtest.Dao.Bean;
import com.example.viewtest.R;

import java.util.List;

public class Adapter_FragmentOne extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int ITEM_HEAD_FRAGMENTONE = 0;
    //分两种item，一种两个图片；一种一个图片
    public static final int ITEM_ONE = 1;//两个图片
    public static final int ITEM_TWO = 2;//一个图片

    private int mHeaderCount = 1;//头部View个数

    private List<Bean> mdata;
    private LayoutInflater mlayoutInflater;
    private Context mcontext;

    public Adapter_FragmentOne() {
    }

    public Adapter_FragmentOne(Context context, List<Bean> list) {
        mcontext = context;
        mlayoutInflater = LayoutInflater.from(context);
        mdata = list;
    }

    //绑定两个图片控件
    public static class OneHolder extends RecyclerView.ViewHolder {
        //        private TextView textView;
        private ImageView imageView1;
        private ImageView imageView2;

        public OneHolder(View item) {
            super(item);
//            textView=(TextView)itemView.findViewById(R.id.rv_item_text);
            imageView1 = (ImageView) itemView.findViewById(R.id.fragmentOne_One_item_image1);
            imageView2 = (ImageView) itemView.findViewById(R.id.fragmentOne_One_item_image2);
        }
    }

    //绑定一个图片控件
    public static class TwoHolder extends RecyclerView.ViewHolder {
        //        private TextView textView;
        private ImageView imageView;

        public TwoHolder(View item) {
            super(item);
//            textView=(TextView)itemView.findViewById(R.id.fragmentOne_Two_item_text);
            imageView = (ImageView) itemView.findViewById(R.id.fragmentOne_Two_item_image);
        }
    }

    public static class HeadFragmentOneHolder extends RecyclerView.ViewHolder {
        public HeadFragmentOneHolder(View item) {
            super(item);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_HEAD_FRAGMENTONE) {
            return new HeadFragmentOneHolder(mlayoutInflater.inflate(R.layout.rv_header, parent, false));
        } else if (viewType == ITEM_TWO) {
            return new TwoHolder(mlayoutInflater.inflate(R.layout.fragment_one_view_two, parent, false));
        } else {
            return new OneHolder(mlayoutInflater.inflate(R.layout.fragment_one_view_one, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeadFragmentOneHolder) {

        } else if (holder instanceof OneHolder) {
//            ((OneHolder) holder).textView.setText(mdata.get(position).getName());
//            ((OneHolder) holder).imageView1.setImageResource(mdata.get(position).getImageId());
//            ((OneHolder) holder).imageView2.setImageResource(mdata.get(position).getImageId());
            Glide.with(mcontext)
                    .load(mdata.get(position).getImageId())
                    .into(((OneHolder) holder).imageView1);
            Glide.with(mcontext)
                    .load(mdata.get(position).getImageId())
                    .into(((OneHolder) holder).imageView2);
        } else {
            ((TwoHolder) holder).imageView.setImageResource(mdata.get(position).getImageId());
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == ITEM_HEAD_FRAGMENTONE) {
            return ITEM_HEAD_FRAGMENTONE;
        } else if (position % 2 == 0) {
            return ITEM_ONE;
        } else {
            return ITEM_TWO;
        }
    }

    @Override
    public int getItemCount() {
        if (mdata != null) {
            return mdata.size() + mHeaderCount;
        }
        return 0;
    }
}
